package com.jftone.bh.app.action;

import com.alibaba.fastjson.JSONObject;
import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.service.OSSFileManagerService;
import com.jftone.bh.app.service.PointService;
import com.jftone.bh.app.util.UploadFile;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.config.Const;
import org.jftone.exception.ActionException;
import org.jftone.util.DataMap;
import org.jftone.util.DateUtil;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Controller(mapping="/appAction")
public class AppAction extends BaseAction {

	private Logger logger = Logger.getLogger(AppAction.class);

	@Autowired
	OSSFileManagerService ossFileService;

	public void index() throws ActionException {
		this.render("app/app_form.html");
	}
	public void uploadFileIos() throws ActionException {
		IData<String, Object> data = getData();
		String imgSrc = data.getString("imgSrc", "");
		try {
			this.putRenderInfo("resultCode", data.getString("resultCode", "-1"));
			this.render("app/app_upload_ios.html");
		} catch (Exception e) {
			logger.error("上传文件页面加载错误" + e.getMessage(), e);
			throw new ActionException("上传文件页面加载错误" + e.getMessage(), e);
		}
	}
	public void uploadFileApk() throws ActionException {
		IData<String, Object> data = getData();
		String imgSrc = data.getString("imgSrc", "");
		try {
			this.putRenderInfo("resultCode", data.getString("resultCode", "-1"));
			this.render("app/app_upload_apk.html");
		} catch (Exception e) {
			logger.error("上传文件页面加载错误" + e.getMessage(), e);
			throw new ActionException("上传文件页面加载错误" + e.getMessage(), e);
		}
	}
	public void upload() throws Exception {
		HttpServletRequest request = getRequest();

		DiskFileItemFactory factory= new DiskFileItemFactory();
		factory.setSizeThreshold(10*1024);	  //默认10K
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding(Const.CHARSET_UTF8);
		upload.setFileSizeMax(1024*1024*100);		//最大文件大小

		IData<String, String> data = new DataMap<String, String>();
		FileItem fileItem = null;
		List<FileItem> listItem = upload.parseRequest(request);
		for (FileItem item : listItem) {
			if(item.isFormField()){
				data.put(item.getFieldName(), item.getString(Const.CHARSET_UTF8));
			}else{
				fileItem = item;
			}
		}
		String type=request.getParameter("type");
		String key,redirectUrl;
		if("1".equals(type)){
			key = "bh.apk";
			redirectUrl ="appAction.do?method=uploadFileApk";
		}else{
			key = "bh.ipa";
			redirectUrl ="appAction.do?method=uploadFileIos";
		}
		String url=ossFileService.upload(fileItem.getInputStream(),"app",key);

		putParam("resultCode", UploadFile.State.OK.getCode());
		putParam("imgSrc", key);
		putParam("resultMsg", UploadFile.State.OK.getMessage());

		this.redirect(redirectUrl+getParam());
	}

}
